////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.display.windows {
	import flash.events.MouseEvent;
	
	import kiwi.events.CallbackEvent;
	
	import plantool.data.Field;
	import plantool.data.Table;
	import plantool.display.windows.objects.ButtonRow;
	import plantool.display.windows.objects.WindowFieldRow;
	import plantool.display.windows.objects.WindowRowSeparator;
	import plantool.events.WindowEvent;

	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class TableRowEditWindow extends BaseWindow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _table:Table		= null;
		private var _data:Object		= null;
		private var _fieldRows:Vector.<WindowFieldRow> = new Vector.<WindowFieldRow>();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function TableRowEditWindow(table:Table, data:*) {
			//store table
			_table 	= table;
			_data 	= data;
			
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function updateData():void {
			//go through all fields
			for each (var field:WindowFieldRow in _fieldRows) {
				if (_data.hasOwnProperty(field.name)) {
					_data[field.name] = field.data;
				}
			}
		}
		
		protected override function leftBtnHandler(e:CallbackEvent):void {
			switch (e.userData.type) {
				//dispose the window on cancel click
				case MouseEvent.CLICK:
					dispose();
					break;
			}
		}
		
		protected override function rightBtnHandler(e:CallbackEvent):void {
			switch (e.userData.type) {
				//call save handler on save click
				case MouseEvent.CLICK:
					updateData();
					save();
					break;
			}
		}
		
		protected override function init():void {
			//call super init
			super.init();
			
			//go through all fields
			for each (var field:Field in _table.fields) {
				//add a row for each field
				var row:WindowFieldRow 	= new WindowFieldRow(this, field);
				row.name				= field.name;
				row.width				= width;
				
				//add row to window
				_fieldRows.push(addChild(row));
			}
			
			//set text on buttonRow
			buttonRow.buttonLeft.text	= "Annuleren";
			buttonRow.buttonRight.text	= "Opslaan";
		}
		
		protected function save():void {
			
		}
	}
}